package m.jclecai.com.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import m.jclecai.com.MyApplication;
import m.jclecai.com.bean.BjdcBean;
import m.jclecai.com.bean.JclqItme;
import m.jclecai.com.bean.LotteryGroupBean;
import m.jclecai.com.bean.ZqItem;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hs014 on 2017/3/2.
 */

public class DataUtil {

    /**
     * 竞彩足球所有的数据
     */
    public static void getJCLQMatchInfo2(TreeMap<String, List<String[]>> treeMap) {
        if(treeMap == null) return;
        List<LotteryGroupBean> groups = new ArrayList<>();
        List<List<ZqItem>> spfchilds = new ArrayList<>();
        // group数据
        for (String key : treeMap.keySet()) {
            List<String[]> strings = treeMap.get(key);
            List<ZqItem> spfItems = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            for (int z = 0; z < strings.size(); z++) {
                map.put(strings.get(z)[18], strings.get(z)[18]);
                spfItems.add(new ZqItem(strings.get(z), false));
                String dg = strings.get(z)[18];

            }
            spfchilds.add(spfItems);
            LotteryGroupBean bean = new LotteryGroupBean();
            bean.setDgValue(map);
            bean.setTitle(key);
            groups.add(bean);
        }
        Collections.sort(groups, new Comparator<LotteryGroupBean>() {
            @Override
            public int compare(LotteryGroupBean o1, LotteryGroupBean o2) {
                return DateUtil.compare(o1.getTitle(), o2.getTitle(), "yyyy-MM-dd") ? 1 : -1;
            }
        });
        Utils.zq_jczqChilds.clear();
        Utils.jclqGroup.clear();
        Utils.jclqGroup.addAll(groups);
        Utils.zq_jczqChilds.addAll(spfchilds);
    }

    public static void getJCLQMatchInfoDG(TreeMap<String, List<String[]>> treeMap) {
        if(treeMap == null) return;
        List<LotteryGroupBean> groups = new ArrayList<>();
        List<List<ZqItem>> spfchilds = new ArrayList<>();
        // group数据
        for (String key : treeMap.keySet()) {
            List<String[]> strings = treeMap.get(key);
            List<ZqItem> spfItems = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            for (int z = 0; z < strings.size(); z++) {
                String dg = strings.get(z)[18];
                if (isDG(dg, "sf")||isDG(dg, "rfsf")||isDG(dg, "sfc")||isDG(dg, "dxf")) {
                    map.put(dg, dg);
                    spfItems.add(new ZqItem(strings.get(z), false));
                }
            }

            if (spfItems.size() > 0) {
                spfchilds.add(spfItems);
                LotteryGroupBean bean = new LotteryGroupBean();
                bean.setDgValue(map);
                bean.setTitle(key);
                groups.add(bean);
            }
        }
        Collections.sort(groups, new Comparator<LotteryGroupBean>() {
            @Override
            public int compare(LotteryGroupBean o1, LotteryGroupBean o2) {
                return DateUtil.compare(o1.getTitle(), o2.getTitle(), "yyyy-MM-dd") ? 1 : -1;
            }
        });
        Utils.zq_jczqChilds.clear();
        Utils.jclqGroup.clear();
        if (groups.size() > 0)
            Utils.jclqGroup.addAll(groups);
        if (spfchilds.size() > 0)
            Utils.zq_jczqChilds.addAll(spfchilds);
    }

    /**
     * 竞彩篮球所有数据
     */
    public static void getJCLQMatchInfo(TreeMap<String, List<String[]>> treeMap) {
        List<LotteryGroupBean> groups = new ArrayList<>();
        Set<String> set = treeMap.keySet();
        Iterator<String> iterator = set.iterator();

        List<List<JclqItme>> sfchilds = new ArrayList<>();
        while (iterator.hasNext()) {
            String key = iterator.next();
            List<String[]> strings = treeMap.get(key);
            List<JclqItme> sfItems = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            for (int z = 0; z < strings.size(); z++) {
                map.put(strings.get(z)[18], strings.get(z)[18]);
                int i = 0;
                JclqItme sfItme = new JclqItme(
                        strings.get(z), false, false, false, false,
                        new LinkedList<String>(), new ArrayList<String>(), new HashMap<Integer, String>(), new HashMap<Integer, String>());
                for (int j = 0; j < Utils.JCLQ_ITEM_LIST
                        .size(); j++) {
                    JclqItme item = Utils.JCLQ_ITEM_LIST
                            .get(j);
                    if (sfItme.getStrings()[1]
                            .equals(item.getStrings()[1])) {
                        i = 1;
                        sfItems.add(item);
                    }
                }
                if (i == 0) {
                    sfItems.add(sfItme);
                }
            }
            sfchilds.add(sfItems);
            LotteryGroupBean bean = new LotteryGroupBean();
            bean.setTitle(key);
            bean.setDgValue(map);
            groups.add(bean);
        }
        Collections.sort(groups, new Comparator<LotteryGroupBean>() {
            @Override
            public int compare(LotteryGroupBean o1, LotteryGroupBean o2) {
                return DateUtil.compare(o1.getTitle(), o2.getTitle(), "yyyy-MM-dd") ? 1 : -1;
            }
        });
        Utils.jclqGroup.clear();
        Utils.jc_jclqChilds.clear();
        Utils.jclqGroup.addAll(groups);
        Utils.jc_jclqChilds.addAll(sfchilds);
    }


    /**
     * 竞彩足球所有的数据
     */
    public static void getJCZQMatchInfo(TreeMap<String, List<String[]>> treeMap) {
        if(treeMap == null) return;
        List<LotteryGroupBean> groups = new ArrayList<>();
        List<List<ZqItem>> spfchilds = new ArrayList<>();
        // group数据
        for (String key : treeMap.keySet()) {
            List<String[]> strings = treeMap.get(key);
            List<ZqItem> spfItems = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            for (int z = 0; z < strings.size(); z++) {
                map.put(strings.get(z)[19], strings.get(z)[19]);
                spfItems.add(new ZqItem(strings.get(z), false));
                String dg = strings.get(z)[19];

            }
            spfchilds.add(spfItems);
            LotteryGroupBean bean = new LotteryGroupBean();
            bean.setDgValue(map);
            bean.setTitle(key);
            groups.add(bean);
        }
        Collections.sort(groups, new Comparator<LotteryGroupBean>() {
            @Override
            public int compare(LotteryGroupBean o1, LotteryGroupBean o2) {
                return DateUtil.compare(o1.getTitle(), o2.getTitle(), "yyyy-MM-dd") ? 1 : -1;
            }
        });
        Utils.zq_jczqChilds.clear();
        Utils.jclqGroup.clear();
        Utils.jclqGroup.addAll(groups);
        Utils.zq_jczqChilds.addAll(spfchilds);
    }

    //支持单关的小蓝点

    /**
     * @param flag     判断单关的参数
     * @param realtype 当前赛事的类型
     * @return
     */
    static boolean isDG(String flag, String realtype) {
        int mid = -1;
        boolean itemDgBool;
        switch (realtype) {
            case "dxf":
            case "bqc":
                mid = 0;
                break;
            case "sfc":
            case "cbf":
                mid = 1;
                break;
            case "rfsf":
            case "jqs":
                mid = 2;
                break;
            case "sf":
            case "rqspf":
                mid = 3;
                break;
            case "spf":
                mid = 4;
                break;
        }
        int value = TextUtils.isEmpty(flag) ? 0 : Integer.parseInt(flag);
        itemDgBool = ((value & 1 << mid) == (1 << mid));    //这是什么意思？
        return itemDgBool;
    }

    //筛选排除单关
    public static void getJCZQMatchInfoDG(TreeMap<String, List<String[]>> treeMap) {
        if(treeMap == null) return;
        List<LotteryGroupBean> groups = new ArrayList<>();
        List<List<ZqItem>> spfchilds = new ArrayList<>();
        // group数据
        for (String key : treeMap.keySet()) {
            List<String[]> strings = treeMap.get(key);
            List<ZqItem> spfItems = new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            for (int z = 0; z < strings.size(); z++) {
                String dg = strings.get(z)[19];
                if (isDG(dg, "rqspf") || isDG(dg, "spf")) {
//                if (isDG(dg, "spf")) {
                    map.put(dg, dg);
                    spfItems.add(new ZqItem(strings.get(z), false));
                }
            }

            if (spfItems.size() > 0) {
                spfchilds.add(spfItems);
                LotteryGroupBean bean = new LotteryGroupBean();
                bean.setDgValue(map);
                bean.setTitle(key);
                groups.add(bean);
            }
        }
        Collections.sort(groups, new Comparator<LotteryGroupBean>() {
            @Override
            public int compare(LotteryGroupBean o1, LotteryGroupBean o2) {
                return DateUtil.compare(o1.getTitle(), o2.getTitle(), "yyyy-MM-dd") ? 1 : -1;
            }
        });
        Utils.zq_jczqChilds.clear();
        Utils.jclqGroup.clear();

        if (groups.size() > 0)
            Utils.jclqGroup.addAll(groups);
        Utils.zq_jczqChilds.addAll(spfchilds);
    }

    public enum LotteryType {
        JCZQ, JCLQ
    }

    /**
     * 将获取到竞彩篮球的数据按照日期进行分类
     *
     * @param response
     * @return
     */
    public static TreeMap<String, List<String[]>> getJCLQData(String response, LotteryType type) {
        List<String[]> obj = FastOk.getList(response, String[].class);
        Map<String, List<String[]>> map = new HashMap<>();
        SimpleDateFormat sDate = new SimpleDateFormat(
                "yyyy-MM-dd");
        for (String[] list : obj) {//遍历这个集合，将他按照日期并以键值对的形式来装
//            String kasaiDate = list[7]; //网页是按照18分组 ios 是 7分组 标题是6
            Date mDate = null;
            try {
                String d = list[7];//zq 时间
                switch (type) {
                    case JCZQ:
                        d = list[18];//zq 时间
                        break;
                    case JCLQ:
                        d = list[17];//lq 时间
                        break;
                }
                mDate = sDate.parse(d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String kasaiDate = sDate.format(mDate.getTime());
            if (map.containsKey(kasaiDate)) {
                List<String[]> b = map.get(kasaiDate);
                b.add(list);
                map.put(kasaiDate, b);
            } else {
                List<String[]> b = new ArrayList<>();
                b.add(list);
                map.put(kasaiDate, b);
            }
        }
        TreeMap<String, List<String[]>> treeMap = new TreeMap<>(
                map);//将这个map集合通过TreeMap来进行排序
        return treeMap;
    }

    /**
     * 将获取到竞彩篮球的数据按照日期进行分类
     *
     * @param response
     * @return
     */
    public static BjdcBean getBJDCData(String response) {
        JSONObject jsonObject = FastOk.get(response, JSONObject.class);
        Map<String, List<String[]>> map = new HashMap<>();
        BjdcBean bjdcBean = new BjdcBean();
        if (jsonObject != null) {
            bjdcBean.setTermNo(jsonObject.getString("termNo"));
            bjdcBean.setTotal(jsonObject.getIntValue("total"));
            String content = jsonObject.getString("content");
            if (!TextUtils.isEmpty(content)) {
                List<String[]> obj = FastOk.getList(content, String[].class);
                SimpleDateFormat sDate = new SimpleDateFormat(
                        "yyyy-MM-dd");
                for (String[] list : obj) {//遍历这个集合，将他按照日期并以键值对的形式来装
//            String kasaiDate = list[7]; //网页是按照18分组 ios 是 7分组 标题是6
                    Date mDate = null;
                    try {
                        String d = list[17];//zq 时间
                        if (TextUtils.isEmpty(d) || d.length() <= 1)
                            d = list[18];
                        mDate = sDate.parse(d);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String kasaiDate = sDate.format(mDate.getTime());
                    if (map.containsKey(kasaiDate)) {
                        List<String[]> b = map.get(kasaiDate);
                        b.add(list);
                        map.put(kasaiDate, b);
                    } else {
                        List<String[]> b = new ArrayList<>();
                        b.add(list);
                        map.put(kasaiDate, b);
                    }
                }
            }
        }
        TreeMap<String, List<String[]>> treeMap = new TreeMap<>(
                map);//将这个map集合通过TreeMap来进行排序
        bjdcBean.setMap(treeMap);
        return bjdcBean;
    }

    /**
     * map 转化为 list
     *
     * @param map
     * @param isKey 是转化key 还是value
     */
    public static List map2List(Map map, boolean isKey) {
        Collection valueCollection = isKey ? map.keySet() : map.values();
        return new ArrayList(valueCollection);
    }
}
